var baseUrl = 'https://api.qikuai100.com/';
$(function() {
	$('.modleDailog').modal("hide");

	var mySwiper = new Swiper('.banner_con', {
		autoplay: 0, //可选选项，自动滑动
		loop: true,
		pagination: '.swiper-pagination',
		onlyExternal: true
	});
	$("#sendCode").click(function() {
		sendCode($("#sendCode"));
	});
	initCatgroyList();
	initHotCompany();
	initOrderList();
	initInforList();
});

var catgeroyList = {};
// 加载资质分类数据
function initCatgroyList() {
	$.ajax({
		type: "POST",
		url: baseUrl + "api/category/list",
		data: {
			"type": 1
		},
		success: function(result) {
			if(result.ret == 0) {
				var dataOne = {
					category_name: result.data[0].category_name,
					id: result.data[0].id,
					subcategoy: []
				};
				var dataTwo = {
					category_name: result.data[1].category_name,
					id: result.data[1].id,
					subcategoy: []
				};
				for(var i = 0;i < result.data[0].subcategoy.length; i++){
					if(i < 9 && result.data[0].subcategoy[i]){
						dataOne.subcategoy.push(result.data[0].subcategoy[i])
					}
				}
				for(var i = 0;i < result.data[1].subcategoy.length; i++){
					if(i < 9 && result.data[1].subcategoy[i]){
						dataTwo.subcategoy.push(result.data[1].subcategoy[i])
					}
				}
				var catgroyOneHtml = template('catgroyOneScript', dataOne);
				var catgroyTwoHtml = template('catgroyTwoScript', dataTwo);
				document.getElementById('catgroyOneHtml').innerHTML = catgroyOneHtml;
				document.getElementById('catgroyTwoHtml').innerHTML = catgroyTwoHtml;

				// 底部查询报价
				catgeroyList = result;
				var chooseOne = template('chooseOneScript', catgeroyList);
				document.getElementById('chooseOne').innerHTML = chooseOne;
			}
		}
	});
};

// 选择资质专业
var twoCatList = {
	name: "资质专业",
	subcategoy: []
};
$('#chooseOne').on("change", "#chooseOneSelect", function() {
	var ss = $(this).children('option:selected').val();
	if(ss) {
		for(var i = 0; i < catgeroyList.data.length; i++) {
			if(ss == catgeroyList.data[i].id) {
				twoCatList.subcategoy = catgeroyList.data[i].subcategoy;
			}
		}
	}
	var chooseTwo = template('chooseTwoScript', twoCatList);
	document.getElementById('chooseTwo').innerHTML = chooseTwo;
});
// 初始化资质专业
var chooseTwo = template('chooseTwoScript', twoCatList);
document.getElementById('chooseTwo').innerHTML = chooseTwo;

// 选择资质等级
var threeCatList = {
	name: "资质等级",
	levels: []
};
$('#chooseTwo').on("change", "#chooseTwoSelect", function() {
	var ssTwo = $(this).children('option:selected').val();
	if(ssTwo) {
		for(var i = 0; i < twoCatList.subcategoy.length; i++) {
			if(ssTwo == twoCatList.subcategoy[i].id) {
				threeCatList.levels = twoCatList.subcategoy[i].levels;
			}
		}
	}
	var chooseThree = template('chooseThreeScript', threeCatList);
	document.getElementById('chooseThree').innerHTML = chooseThree;
});
// 初始化资质专业
var chooseThree = template('chooseThreeScript', threeCatList);
document.getElementById('chooseThree').innerHTML = chooseThree;

// 跳转
function goGetDt() {
	var idOne = $('#chooseOneSelect').val(),
		idTwo = $('#chooseTwoSelect').val(),
		idThree = $('#chooseThreeSelect').val();
	var num = $('#chooseThreeSelect').children("option").size();
	if(idOne == "") {
		alert("请选择资质类别");
		return;
	}
	if(idTwo == "") {
		alert("请选择资质专业");
		return;
	}
	if(num != 1 && idThree == "") {
		alert("请选择资质等级");
		return;
	}
	location.href = "/quota/getDetail/" + idOne + "/" + idTwo + "/" + idThree;
}

// 加载首页订单信息
function initOrderList() {
	$.ajax({
		type: "POST",
		url: baseUrl + "api/order/list",
		data: {
			"page": 1
		},
		success: function(result) {
			if(result.ret == 0) {
				var dataJson = result.data;

				var orderListHtml = template('orderListScript', dataJson);
				document.getElementById('orderListHtml').innerHTML = orderListHtml;
				var mySwiperOrder = new Swiper('.orderSwiper', {
				    autoplay: 3500,//可选选项，自动滑动
				    loop: true,  //是否循环
					slidesPerView : 4,  //可视区域显示几个
					direction: 'vertical'
				})
			}
		}
	});
};

// 加载资讯信息
function initInforList() {
	$.ajax({
		type: "POST",
		url: baseUrl + "api/index/news",
		success: function(result) {
			if(result.ret == 0) {
				var datInfor = result.data;
				var inforHtmlTepl = template('infoScriptTemp', datInfor);
				document.getElementById('inforHtmlTepl').innerHTML = inforHtmlTepl;
				var mySwiper = new Swiper('.infoBanner', {
					autoplay: 4500, //可选选项，自动滑动
					loop: true,
					nextButton: '.swiper-button-next',
					prevButton: '.swiper-button-prev',
					onlyExternal: true,
					speed: 800
				});
			}
		}
	});
};

// 加载 热门公司
function initHotCompany() {
	$.ajax({
		type: "POST",
		url: baseUrl + "api/company/list",
		data: {},
		success: function(result) {
			if(result.ret == 0) {
				var hotCompantHtml = template('hotCompantScript', result);
				document.getElementById('hotCompantHtml').innerHTML = hotCompantHtml;
			}
			var mySwiper = new Swiper('.smallBanner', {
				autoplay: 3000,
				loop: false,
				slidesPerView: 4,
				spaceBetween: 10,
				nextButton: '.swiper-button-next',
				prevButton: '.swiper-button-prev',
				onlyExternal: true
			});
		}
	});
};


/**
 *弹层传递数据 *
 */
function values(ID) {
	$('#dateId').val(ID);
}

//锚点滚动
$('a[href*=#],area[href*=#]').click(function() {
	if(location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
		var $target = $(this.hash);
		$target = $target.length && $target || $('[name=' + this.hash.slice(1) + ']');
		if($target.length) {
			var targetOffset = $target.offset().top;
			$('html,body').animate({
					scrollTop: targetOffset
				},
				500);
			return false;
		}
	}
});

// 滚动事件监听
$(window).scroll(function() {
	var top = $(document).scrollTop();
	if(top >= 800) {
		$('.leftFixedNav').show();
	} else {
		$('.leftFixedNav').hide();
	}
});